package com.neutech.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.neutech.entity.HomeWork;
import com.neutech.mapper.HomeWorkMapper;
import com.neutech.service.HomeWorkService;
import com.neutech.service.StudentService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Date;

/**
 * 作业 服务层实现。
 *
 * @author Administrator
 * @since 2025-04-30
 */
@Service
public class HomeWorkServiceImpl extends ServiceImpl<HomeWorkMapper, HomeWork>  implements HomeWorkService{

    @Resource
    StudentService studentService;
    @Override
    public boolean add(HomeWork homeWork) {
        homeWork.setCreateDate(LocalDate.now());
        homeWork.setCreateTime(LocalTime.now());
        homeWork.setCount((int)studentService.count(homeWork.getGradeId()));
        homeWork.setCommitCount(0);
        homeWork.setFormat("");
        return this.save(homeWork);
    }

    @Override
    public Page<HomeWork> list(Integer pageNo, Integer pageSize, String name, Integer active) {
        QueryWrapper wrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        if (null != active) {
            wrapper.eq("active", active);
        }
        wrapper.orderBy("id", false);
        return this.page(new Page<>(pageNo, pageSize), wrapper);
    }
}
